package com.leonwoodstudio.endorsementclient;

import android.os.Handler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by win7 on 2017/3/17.
 */
public class ServerSocketThread extends Thread {
    int listen_port;
    Handler handler;
    public ServerSocketThread(int listen_port, Handler handler) {
        this.listen_port = listen_port;
        this.handler = handler;
    }

    @Override
    public void run() {
        super.run();
        ServerSocket server_socket = null;
        try {
            server_socket = new ServerSocket(listen_port);
            while (true) {
                //监听连接 ，如果无连接就会处于阻塞状态，一直在这等着
                Socket client_socket = server_socket.accept();
                new ReceiveFileThread(client_socket, handler).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
            // listen socket exception, kill myself...
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}
